<template>
  <el-main>
    <!-- Main content -->
    <!-- 搜索栏 -->
    <el-form ref="searchForm" :model="searchForm" :inline="true" size="small">
      <el-form-item prop="courseName">
        <el-input v-model="searchForm.courseName" placeholder="请输入课程名称" />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button style="color:#FF7670;" icon="el-icon-close" @click="resetBtn">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 课程表格列表 -->
    <el-table :data="tableList" :height="tableHeight" border stripe>
      <el-table-column align="center" prop="className" label="班级名称" />
      <el-table-column align="center" prop="courseName" label="课程名称" />
      <el-table-column align="center" prop="courseYear" label="开课年份" />
      <el-table-column align="center" prop="classYear" label="招生年份" />
      <el-table-column align="center" prop="type" label="开课学期">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type==='0'" type="success" size="small">春季</el-tag>
          <el-tag v-if="scope.row.type==='1'" type="danger" size="small">秋季</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300">
        <template slot-scope="scope">
          <div class="myBtn">
            <el-button style="margin-right: 15px;" type="success" size="small" icon="el-icon-plus" @click="exportStu(scope.row)">导出学生</el-button>
            <el-upload
              class="upload-demo"
              action=""
              :auto-upload="true"
              :multiple="false"
              :show-file-list="false"
              :http-request="uploadFileFn"
              :file-list="fileList"
              :before-upload="
                file => {
                  beforeUpload(file, scope.row)
                }
              "
            >
              <el-button type="primary" size="small" icon="el-icon-plus">导入成绩</el-button>
            </el-upload>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件 -->
    <el-pagination
      :current-page.sync="searchForm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="searchForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="searchForm.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />
  </el-main>

</template>

<script>
import { getTeacherClassCourseListApi, importStuInfoApi } from '@/api/teacher'
import { getUserId } from '@/utils/auth'
export default {
  data() {
    return {
      param: {
        courseId: '',
        classId: ''
      },
      fileList: [],
      // 搜索参数设置
      searchForm: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        courseName: '',
        teacherId: getUserId()
      },
      // 表格属性
      tableHeight: 0,
      // 表格数据
      tableList: []
    }
  },
  created() {
    this.getTeacherClassCourseList()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 220
    })
  },
  methods: {
    // 文件校验方法
    beforeUpload(file, row) {
      this.param.classId = row.classId
      this.param.courseId = row.courseId
      // console.log('文件上传之前')
      // console.log(row)
      // 通过split方法获取到文件的后缀名
      const fileArr = file.name.split('.')
      const suffix = fileArr[fileArr.length - 1]
      // 只能导入.xls和.xlsx文件
      if (!/(xls|xlsx)/i.test(suffix)) {
        this.$message.warning('文件格式不正确')
        return false
      }
      // 不能导入大小超过2Mb的文件
      if (file.size > 10 * 1024 * 1024) { // 注意单位换算，2MB = 2 * 1024 * 1024 字节
        this.$message('文件过大，请上传小于10MB的文件～')
        return false
      }
      return true
    },
    // 文件发生改变就会触发的事件
    async uploadFileFn(param) {
      const file = param.file
      const formData = new FormData()
      formData.append('file', file) // 确保键是字符串，并且和param.file的键一致
      formData.append('classId', this.param.classId)
      formData.append('courseId', this.param.courseId)
      console.log(formData)
      const res = await importStuInfoApi(formData) // 假设importStuInfoApi是处理文件上传的API函数
      // console.log(res)
      if (res && res.code === 200) {
        this.$message.success(res.msg)
      }
    },
    async exportStu(row) {
      const abtn = document.createElement('a')
      abtn.href = process.env.VUE_APP_BASE_API_PRO + '/api/teacher/exportStuInfo?classId=' + row.classId
      abtn.click()
    },
    async getTeacherClassCourseList() {
      const res = await getTeacherClassCourseListApi(this.searchForm)
      debugger
      if (res && res.code === 200) {
        this.tableList = res.data.records
        this.searchForm.total = res.data.total
      }
    },
    // 页容量变化
    sizeChange(val) {
      this.searchForm.pageSize = val
      this.getTeacherClassCourseList()
    },
    // 当前页变化
    currentChange(val) {
      this.searchForm.currentPage = val
      this.getTeacherClassCourseList()
    },
    // 搜索按钮
    searchBtn() {
      this.getTeacherClassCourseList()
    },
    // 重置按钮
    resetBtn() {
      this.searchForm.courseName = ''
      this.searchForm.currentPage = 1
      this.searchForm.pageSize = 10
      this.getTeacherClassCourseList()
    }
  }
}
</script>

<style lang="scss" scoped>
.myBtn {
  display:flex;
  align-items: center;
  justify-content: center;
}
</style>
